package com.yhh.zhongdian.widget.modialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yhh.zhongdian.R;
import com.yhh.zhongdian.utils.MarkdownUtils;
import com.yhh.zhongdian.utils.VersionUtil;
import com.yhh.zhongdian.widget.views.ATEAutoCompleteTextView;

import java.io.File;

/**
 * 对话框
 */
public class ApkDownDialog extends BaseDialog {
    private TextView tvTitle;
    private TextView tvContent;
    private ProgressBar progressBar;
    private TextView tvOk;

    public static ApkDownDialog builder(Context context) {
        return new ApkDownDialog(context);
    }

    private ApkDownDialog(Context context) {
        super(context, R.style.alertDialogTheme);
        @SuppressLint("InflateParams") View view = LayoutInflater.from(context).inflate(R.layout.dialog_download, null);
        setContentView(view);
        bindView(view);
    }

    public ApkDownDialog setTitle(String title) {
        tvTitle.setText(title);
        return this;
    }

    private void bindView(View view) {
        View llContent = view.findViewById(R.id.ll_content);
        llContent.setOnClickListener(null);
        tvTitle = view.findViewById(R.id.tv_title);
        tvTitle.setText("版本升级");
        tvContent = view.findViewById(R.id.tv_content);
        tvContent.setText("当前有新版本可以升级哦！");
        progressBar = view.findViewById(R.id.pb_progress);
        tvOk = view.findViewById(R.id.tv_ok);

    }

    public ApkDownDialog setClick(String content, String downloadUrl, SimpleCallback callback) {
        MarkdownUtils.setText(tvContent, content);
        tvOk.setOnClickListener(v -> {
            callback.click(downloadUrl);
            this.dismiss();
        });
        return this;
    }

    public ApkDownDialog setCallback(String content, String downloadUrl, String path, String name, ApkDownDialog.Callback callback) {
        MarkdownUtils.setText(tvContent, content);
        tvOk.setOnClickListener(v -> {
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setProgress(0);
            tvOk.setVisibility(View.GONE);
            VersionUtil.download(downloadUrl, path, name, callback);
        });
        return this;
    }


    public void updateProgress(int progress) {
        progressBar.setProgress(progress);
    }

    /**
     * 输入book地址确定
     */
    public interface Callback {
        /**
         * @param file 下载成功后的文件
         */
        void onDownloadSuccess(File file);

        /**
         * @param progress 下载进度
         */
        void onDownloading(int progress);

        /**
         * @param e 下载异常信息
         */
        void onDownloadFailed(Exception e);
    }

    public interface SimpleCallback {
        void click(String url);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return true;
    }
}
